package mydesign.chapters02_observer.v1;


import mydesign.chapters02_observer.v1.Observer.Subscriber;

import java.util.ArrayList;
import java.util.List;

/**
 * 报社
 */
public class Newspaper  implements Subject{


    private List<Subscriber> observerList;
    private String paperInfo;


    public Newspaper(){
        observerList = new ArrayList();
    }

    @Override
    public void registerOberserver(Subscriber subscriber) {
        observerList.add(subscriber);
    }

    @Override
    public void removeOberserver(Subscriber subscriber) {
        for (int i = 0; i < observerList.size(); i++) {
            Subscriber teampsubscriber = observerList.get(i);
            if(teampsubscriber.toString().equals(subscriber.toString())){
                System.out.println("删除订阅者:"+teampsubscriber.toString());
                observerList.remove(i);

            }
        }


    }

    /**
     * 报纸信息
     * @param paperInfo
     */
    @Override
    public void setChange(String paperInfo){
        this.paperInfo = paperInfo;
        nofifyOberserver();
    }



    @Override
    public void nofifyOberserver() {
        for (int i = 0; i <observerList.size() ; i++) {
            Subscriber subscriber = observerList.get(i);
            subscriber.updateInfo(paperInfo);
        }

    }
}
